<?php
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\ActivityLogModel;
use app\portal\model\ActivityPreLogModel;
use app\portal\model\ActivityUploadModel;
use app\portal\model\PointUseLogModel;
use app\portal\model\UserModel;
use think\Db;
use Firebase\JWT\JWT;
use EasyWeChat\Factory;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/24
 * Time: 16:49
 */
class AdminWeixinController extends AdminBaseController{
    //扫码打卡记录
    public function index(){
        $where=[];
        $model=new ActivityLogModel();
        $user =new UserModel();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $user_id=$user->where('user_nickname','like','%'.$param['keyword'].'%')->column('id');
            $where[]=['user_id','in',$user_id];
            $this->assign('keyword',$param['keyword']);
        }
        $data=$model->with('user')->with('activity')->where($where)->order('id desc')->paginate(10);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //活动报名记录
    public function act_pre_log(){
        $where=[];
        $model=new ActivityPreLogModel();
        $user =new UserModel();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $user_id=$user->where('user_nickname','like','%'.$param['keyword'].'%')->column('id');
            $where[]=['user_id','in',$user_id];
            $this->assign('keyword',$param['keyword']);
        }
        $data=$model->with('user')->with('activity')->where($where)->order('id desc')->paginate(10);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //报名记录删除
    public function act_pre_log_del(){
        $Model = new ActivityPreLogModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }
    //销分记录
    public function pointslog(){
        $where=[];
       $model=new PointUseLogModel();
        $user =new UserModel();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $user_id=$user->where('user_nickname','like','%'.$param['keyword'].'%')->column('id');
            $where[]=['user_id','in',$user_id];
            $this->assign('keyword',$param['keyword']);
        }
        $data=$model->with('user')->where($where)->order('id desc')->paginate(10);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //志愿者上传图文记录
    public function upload(){
        $where=[];
        $org_id=input('id');
        $model= new ActivityUploadModel();
        $user =new UserModel();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $user_id=$user->where('user_nickname','like','%'.$param['keyword'].'%')->column('id');
            $where[]=['user_id','in',$user_id];
            $this->assign('keyword',$param['keyword']);
        }
        $data=$model->with('activity')->with('user')->where('org_id',$org_id)->where($where)->paginate(10);
        $name=Db::name('organize')->where('id',$param['id'])->value('name');
        $this->assign('org_id',$org_id);
        $this->assign('data',$data);
        $this->assign('name',$name);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    public function rule_del(){

    }
    //通过
    public function pass(){
        $Model = new ActivityUploadModel();
        $rule_point=Db::name('point_rule')->where('org_id',2)->value('num');
        $id= $this->request->param('id');
        $user_id=$this->request->param('user_id');
        $data=['status'=>1];
        $result=$Model->where('id',$id)->update($data);
        if ($result) {
            Db::name('user_rank')->where('user_id',$user_id)->setInc('z_times',1);
            Db::name('user_rank')->where('user_id',$user_id)->setInc('all_points',1/$rule_point);
            Db::name('user_rank')->where('user_id',$user_id)->setInc('can_points',1/$rule_point);
            $this->success('确认完成成功！');
        } else {
            $this->error('确认失败');
        }
    }
    public function test(){


    }
    //打卡记录删除
    public function activity_log_del(){
        $Model = new ActivityLogModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }
    //上传记录删除
    public function upload_del(){
        $Model = new ActivityUploadModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }
    public function check_points_page(){
        echo '<a href="check_points" target="_blank">去扫码</a>';
    }
    public function check_points(){
        $model=new PointUseLogModel();
        $currentUrl = urlencode($this->request->url(true));
        $config=[
            'app_id'=>'wxd65c593552d40442',
            'secret'=>'99bc505a01f458c459590aa1b1875aec',
            'token'=>'',
            'aes_key'=>'',
            'response_type'=>'array',
            'log'=>[
                'level'=>'debug',
            ],
            'oauth'=>[
                'scopes'=>['snsapi_userinfo'],
                'callback'=>'http://rzjj.coolaring.com/portal/admin_weixin/oauth_callback?target='.$currentUrl
            ]
        ];
        $this->app = Factory::officialAccount($config);
        $this->assign('app',$this->app);
        $admin_id=session('ADMIN_ID');
        $user_login=Db::name('user')->where('id',$admin_id)->value('user_login');
        $times=Db::name('point_use_log')->where('operator',$admin_id)->count();
        $points=Db::name('point_use_log')->where('operator',$admin_id)->sum('point');
        $log=$model->with('user')->where('operator',$admin_id)->order('id desc')->select();
        $this->assign([
           'user_login'=>$user_login,
            'times'=>$times,
            'points'=>$points,
            'log'=>$log
        ]);
        return $this->fetch();
    }
    public function ajax_get_points(){
        $result=[];
        try{
            $key = "rzjj";
            $jwt=new JWT;
            $token=$this->request->param('check_code');
            $obj =$jwt->decode($token,$key,["HS256"]);
            $code_info=Db::name('point_exchange_log')->where('code',$token)->find();
            if(!$code_info){
                throw new \Exception('查不到该条信息~');
            }
            if($code_info['status']==1){
                throw new \Exception('该销分券已经使用~');
            }
            $data['id']=$code_info['id'];
            $data['point']=$code_info['point'];
            $data['car_num']=$obj->car_num;
            $result["error"]= false;
            $result['errmsg']='success';
            $result['data']=$data;

        }catch (\Exception $e){
            $result["error"]= true;
            $msg= "[{$e->getLine()}]".$e->getMessage();
            $result['errmsg']=$msg;
        }
        return json($result);
    }
    //处理确认完成销分操作
    public function do_check_points(){
        $result=[];
        $id=input('get.id');
        try{
            Db::name('point_exchange_log')->where('id',$id)->update(['status'=>1]);
            $find=Db::name('point_exchange_log')->where('id',$id)->field('user_id,point')->find();
            Db::name('point_use_log')->insert(['user_id'=>$find['user_id'],'point'=>$find['point'],'ctime'=>time(),'operator'=>cmf_get_current_admin_id()]);
            $result["error"]= false;
            $result["errmsg"]='使用成功';
        }catch(\Exception $e){
            $result["error"]= true;
            $msg= "[{$e->getLine()}]".$e->getMessage();
            $result['errmsg']=$msg;
        }
        return json($result);
    }

    public function oauth_callback(){
        $target_url = $this->request->param('target','');
        $targetUrl = empty($target_url) ? '/mobile/' : $target_url;
        header('location:'. $targetUrl);

    }
    //销分记录删除
    public function point_use_log_del(){
        $Model = new PointUseLogModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }
}